package sitedao

import (
	"hft/Http/Models/dao"
	siterequest "hft/Http/Services/site/site_request"

	"gorm.io/gorm"
)

type Site struct{}

func (s *Site) GetPhone(phone string, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if phone == "" {
			return db
		} else {
			return db.Where("hft_accounts.phone = ?", phone)
		}
	}
}

func (s *Site) GetUsername(username string, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if username == "" {
			return db
		} else {
			return db.Where("hft_accounts.username = ?", username)
		}
	}
}

func (s *Site) GetRealName(real_name string, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if real_name == "" {
			return db
		} else {
			return db.Where("hft_accounts.real_name = ?", real_name)
		}
	}
}

func (s *Site) ID(id int, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if id == 0 {
			return db
		} else {
			return db.Not("hft_accounts.id = ?", id)
		}
	}
}

func (s *Site) Join(db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Select("hft_accounts.*,r.manage_id").Joins("LEFT JOIN hft_regions r ON r.manage_id = hft_accounts.id").Group("hft_accounts.id")
	}
}

func (s *Site) List(paginate *siterequest.SiteReq, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Scopes(s.Join(db)).Scopes(s.GetPhone(paginate.Phone, db)).Scopes(s.GetRealName(paginate.RealName, db)).Scopes(s.GetUsername(paginate.Username, db)).Scopes(dao.Paginate(dao.Page(paginate.Page), db)).Where("hft_accounts.role_id = ? AND hft_accounts.status = 1", 32)
	}
}

var SiteDao = new(Site)
